<?php

Class Storage{
    
    var $_Podkova;
    var $_Mortar;
    var $_Artifact;
    var $_Clothing;
    var $_Boiler;
    var $_Seed;
    var $_Skin;
    #var $_Food;
    var $ingr;
    var $elixir;
    
    private $_Goods;
    
        
    function present($friend, $good, $parent){
        switch($good){
            case "artifact":
            case "clothing":
                if(!$this->_Goods) $this->_Goods = new _Goods();
                return $this->_Goods->add($friend, $parent, $good);
            break;
            case "ingredient":
                if(!$this->ingr)  $this->ingr = new Ingredient();
                return $this->ingr->addIngrient($friend, 
                                    Habars::$elements[$parent]['parent'], 
                                    Habars::$elements[$parent]['type'], $parent, 
                                    1);
            case "potion":                
                if(!$this->elixir)  $this->elixir = new Elixir();                 
                return $this->elixir->add($friend, $parent);                        
            break;
        }
    }
    
    
    function add($user, $good, $id, $otherparams = array()){
        #return $good;
        switch($good){
            case "skin":
                #return "jkhkh";
                if(!$this->_Goods) $this->_Goods = new _Goods();
                if( $this->_Goods->_isit($user, $id, "skin")){
                    Connect::_error("У Вас уже есть Скин. Желаете продать и купить новый? ");
                    return false;
                }
                return $this->_Goods->add($user, $id, "skin");
            break;
            case "podkova":
                #return "jkhkh";
                if(!$this->_Goods) $this->_Goods = new _Goods();
                if( $this->_Goods->_isit($user, $id, "podkova")){
                    Connect::_error("У Вас уже есть Подкова. Желаете продать и купить новую? ");
                    return false;
                }
                return $this->_Goods->add($user, $id, "podkova");
            break;
            case "mortar":
                if(!$this->_Goods) $this->_Goods = new _Goods();
                if( $this->_Goods->_isit($user, $id, "mortar")){
                    Connect::_error("У Вас уже есть ступка. Желаете продать и купить новую? ");
                    return false;
                }
                return $this->_Goods->add($user, $id, "mortar");
            break;
            case "artifact":
                if(!$this->_Goods) $this->_Goods = new _Goods();
                if( $this->_Goods->_isit($user, $id, "artifact")){
                    Connect::_error("У Вас уже есть такой артифакт. Выберите другой?");
                    return false;
                }
                return $this->_Goods->add($user, $id, "artifact");             
            break;
            case "clothing":
                if(!$this->_Goods) $this->_Goods = new _Goods();
                if( $this->_Goods->_isit($user, $id, "clothing")){
                    Connect::_error("У Вас уже есть такая шмотка. Выберите другую?");
                    return false;
                }
                return $this->_Goods->add($user, $id, "clothing");
            break;
            case "boiler":
                if(!$this->_Goods) $this->_Goods = new _Goods();
                if( $this->_Goods->_isit($user, $id, "boiler")){
                    Connect::_error("У Вас уже есть котёл. Желаете продать и купить новый? ");
                    return false;
                }
                return $this->_Goods->add($user, $id, "boiler");
            break;
            case "ingredient":
                Habars::getElement($id);
                /*
                if(!isset(Habars::$elements[$id])){
                    Connect::_error("Ингредиент не найден");
                    return false;
                }
                */
                if(!$this->ingr)  $this->ingr = new Ingredient();
                #return true;                  
                return $this->ingr->addIngrient($user, 
                                    Habars::$elements[$id]['parent'], 
                                    Habars::$elements[$id]['type'], $id, 
                                    @$otherparams[0]);        
            break;
            case "potion":                
                /*
                if(!isset(Book::$greens[$id])){
                    Connect::_error("Эликсир не найден");
                    return false;
                }
                */
                #return "true";
                if(!$this->elixir)  $this->elixir = new Elixir();                 
                return $this->elixir->add($user, $id, true, @$otherparams[0]);
                        
            break;
            case "seed":
                if(!$this->_Seed)  $this->_Seed = new _Seed();
                return $this->_Seed->add($user, $id, @$otherparams[0]);
            break;
            case "food":
                if(!$this->_Goods) $this->_Goods = new _Goods();                
                return $this->_Goods->add($user, $id, "food", false, @$otherparams[0]);
                
                /*if(!$this->_Food)  $this->_Food = new _Food();
                return $this->_Food->add($user, $id);*/
                
            break;
        }        
    }
    
    function isit($user, $good, $id, $count = 1){
        switch($good){
            case "skin":
                if(!$this->_Goods) $this->_Goods = new _Goods();
                return $this->_Goods->_isit($user, $id, "skin", $count);
            break;
            case "podkova":
                if(!$this->_Goods) $this->_Goods = new _Goods();
                return $this->_Goods->_isit($user, $id, "podkova", $count);
            break;
            case "mortar":
                if(!$this->_Goods) $this->_Goods = new _Goods();
                return $this->_Goods->_isit($user, $id, "mortar", $count);
            break;
            case "artifact":
                if(!$this->_Goods) $this->_Goods = new _Goods();
                return $this->_Goods->_isit($user, $id, "artifact", $count);               
            break;
            case "clothing":
                if(!$this->_Goods) $this->_Goods = new _Goods();
                return $this->_Goods->_isit($user, $id, "clothing", $count);
            break;
            case "boiler":
                if(!$this->_Goods) $this->_Goods = new _Goods();
                return $this->_Goods->_isit($user, $id, "boiler", $count);
            break;
            case "ingredient":
                Habars::getElement($id);
                /*
                if(!isset(Habars::$elements[$id])){
                    Connect::_error("Ингредиент не найден");
                    return false;
                }
                */
                if(!$this->ingr)  $this->ingr = new Ingredient();                 
                #return $this->ingr->isIngridient($user, array('count' => 1, 'habar' => $id));
                return $this->ingr->_isit($user, $id, $count);        
            break;
            case "potion":                
                /*
                if(!isset(Book::$greens[$id])){
                    Connect::_error("Эликсир не найден");
                    return false;
                }
                */
                #return 1;
                if(!$this->elixir)  $this->elixir = new Elixir();                 
                return $this->elixir->_isit($user, $id, $count);
                        
            break;
            case "food":                                
                if(!$this->_Goods) $this->_Goods = new _Goods();
                return $this->_Goods->_isit($user, $id, "food", $count);                
                /*if(!$this->_Food)  $this->_Food = new _Food();
                return $this->_Food->_isit($user, $id);*/        
            break;
        }        
    }    
    
    function del($user, $good, $id, $count = 1){
        switch($good){
            case "skin":
            case "podkova":
            case "mortar":
            case "boiler":
                if(!$this->_Goods) $this->_Goods = new _Goods();
                return $this->_Goods->_del($user, $id, $good );
            break;            
            
            case "artifact":
                if(!$this->_Goods) $this->_Goods = new _Goods();
                return $this->_Goods->del($user, $id, "artifact", 0, $count);               
            break;
            case "clothing":
                if(!$this->_Goods) $this->_Goods = new _Goods();
                return $this->_Goods->del($user, $id, "clothing", 0, $count); 
            break;            
            case "ingredient":
                Habars::getElement($id);                
                if(!$this->ingr)  $this->ingr = new Ingredient();                                         
                return $this->ingr->del($user, $id, $count);
            break;
            case "potion":                                
                if(!$this->elixir)  $this->elixir = new Elixir();                       
                return $this->elixir->del($user, $id, $count);                        
            break;
            case "seed":                
                if(!$this->_Seed)  $this->_Seed = new _Seed();  
                _Goods::$goods[$good][$id]['count']--;                
                return $this->_Seed->del($user, $id, $count);
                        
            break;
            case "food":                
                if(!$this->_Goods) $this->_Goods = new _Goods();
                return $this->_Goods->del($user, $id, "food", 0, $count);
                        
            break;
        }        
    }
    
    function getOne($user, $good, $id){
        return $this->getMy($user, $good, $id);
    }
    
    function getUse($user, $good, $id = false){
        return $this->getMy($user, $good, $id, true);
    }
    
    function getMy($user, $good, $id = false, $use = false){
        switch($good){
            case "skin":
            case "podkova":
            case "mortar":
            case "boiler":            
                if(!$this->_Goods) $this->_Goods = new _Goods();                                
                $this->_Goods->getMy($user, $id, $good);                
                #if( !isset($_SESSION['_goods'][$good]) &&  count($_SESSION['_goods'][$good]) == 0 ) return array() ;                
                if( !isset(_Goods::$goods[$good]) &&  count(_Goods::$goods[$good]) == 0 ) return array() ;
                #return $_SESSION['_goods'][$good];                
                return _Goods::$goods[$good];
            break;
            
            #parent
            case "food":                
                _echo('food');
                if(!$this->_Goods) $this->_Goods = new _Goods();                
                $this->_Goods->getMy($user, $id, "food");
                #if( count($_SESSION['_goods']["food"]) == 0 ) return false ;
                if( count(_Goods::$goods["food"]) == 0 ) return false ;
                if(!Goods::getAll()) return false;

                $_arr = array() ;
                foreach(_Goods::$goods["food"] as $key => $val){
                    
                    if($_arr[$val['parent']]){
                        $_arr[$val['parent']]['count']++;    
                    }else{
                        $_arr[$val['parent']] = array();
                        $_arr[$val['parent']]['id'] = $val['id'];
                        $_arr[$val['parent']]['count'] = 1;   
                        $_arr[$val['parent']]['type'] = $good;
                        $_arr[$val['parent']]['parent'] = $val['parent'];                                  
                    }  
                }
                if($id){
                    return @$_arr[$id];
                }                
                return $_arr;                                             
            break;                        
            
                        
            case "artifact":            
            case "clothing":              
                if(!$this->_Goods) $this->_Goods = new _Goods();                
                $this->_Goods->getMy($user, $id, $good);
                #if( count($_SESSION['_goods'][$good]) == 0) return array() ;
                if( count(_Goods::$goods[$good]) == 0) return array() ;
                if(!Goods::getAll()) return false;
                $_arr = array() ;
                #foreach($_SESSION['_goods'][$good] as $key => $val){
                foreach(_Goods::$goods[$good] as $key => $val){
                    
                    if($_arr[$val['parent']]){                        
                        if(!$use){
                            if($val['use'] == 0){
                                $_arr[$val['parent']]['count']++;        
                            }
                        }else{
                            if($val['use'] == 1){
                                $_arr[$val['parent']]['count']++;        
                            }
                        }            
                    }else{
                        if(!$use){
                            if($val['use'] == 0){
                                $_arr[$val['parent']] = array();
                                $_arr[$val['parent']]['id'] = $val['id'];
                                $_arr[$val['parent']]['count'] = 1;   
                                $_arr[$val['parent']]['type'] = $good;
                                $_arr[$val['parent']]['parent'] = $val['parent']; 
                                $_arr[$val['parent']]['idM'] = $val['idM'];        
                            }
                        }else{
                            if($val['use'] == 1){                                
                                $_arr[$val['parent']] = array();
                                $_arr[$val['parent']]['id'] = $val['id'];
                                $_arr[$val['parent']]['count'] = 1;   
                                $_arr[$val['parent']]['type'] = $good;
                                $_arr[$val['parent']]['parent'] = $val['parent']; 
                                $_arr[$val['parent']]['idM'] = $val['idM'];        
                            }
                        }
                    }  
                }
                if($id){
                    foreach($_arr as $k => $v){
                        if(intval($v['parent'] ) != $id ){
                            unset($_arr[$k]);
                        } 
                    }
                }                
                return $_arr;               
            break;                       
            case "ingredient":                                
                if(!$this->ingr)  $this->ingr = new Ingredient();
                $arr = $this->ingr->getIngridients($user);     
                if( $arr === false ) return false ;                
                return $arr;                    
            break;
            
            #parent
            case "potion":                                
                if(!$this->elixir)  $this->elixir = new Elixir();
                $arr = $this->elixir->getMy($user, $id);                
                if( $arr === false ) return false ;
                $_arr = array() ;
                foreach($arr as $key => $val){                    
                    if($_arr[$val['parent']]){
                        $_arr[$val['parent']]['count']++;    
                    }else{
                        $_arr[$val['parent']] = array();
                        $_arr[$val['parent']]['count'] = 1;   
                        #Куплен(1) или сварен(0) эликсир
                        $_arr[$val['parent']]['bybuy'] = $val['bybuy'];
                        $_arr[$val['parent']]['type'] = $good;
                        $_arr[$val['parent']]['parent'] = $val['parent'];
                        $_arr[$val['parent']]['id'] = $val['id'];
                    }  
                }
                return $_arr;                
                        
            break;
            case "seed":                                
                if(!$this->_Seed)  $this->_Seed = new _Seed();
                $arr = $this->_Seed->getMy($user, $id);  
                #return $arr;    
                if( $arr === false ) return false ;                
                $_arr = array();
                _echo(1);
                #return $arr;
                foreach($arr as $key => $val){                    
                    #_echo("seed >> ". $val);
                    if($_arr[$val['parent']]){
                        #Что не используется
                        if(!$use){
                            if($val['use'] == 0){
                                $_arr[$val['parent']]['count']++;        
                            }
                        }else{
                            if($val['use'] == 1){
                                $_arr[$val['parent']]['count']++;        
                            }
                        }  
                    }else{
                        if(!$use){
                            if($val['use'] == 0){
                                $_arr[$val['parent']] = array();
                                $_arr[$val['parent']]['count'] = 1;   
                                $_arr[$val['parent']]['type'] = $good;
                                $_arr[$val['parent']]['parent'] = $val['parent'];
                                $_arr[$val['parent']]['id'] = $val['id'];          
                            }
                        }else{
                            if($val['use'] == 1){
                                $_arr[$val['parent']] = array();
                                $_arr[$val['parent']]['count'] = 1;   
                                $_arr[$val['parent']]['type'] = $good;
                                $_arr[$val['parent']]['parent'] = $val['parent'];
                                $_arr[$val['parent']]['id'] = $val['id'];     
                            }
                        }
                        
                    }  
                }
                return $_arr;            
                #return $this->ingr->delIngridient($user, array('count' => 1, 'habar' => $id));        
            break;
            
        }
    }
    
    function _unuse($user, $good, $id){
        return $this->_use($user, $good, $id, 0, true);
    }
    
    function _use($user, $good, $id, $idM = 0, $unuse = false ){
        switch($good){
            case "skin":
            case "podkova":
            case "mortar":
            case "boiler":
                if(!$this->_Goods)  $this->_Goods = new _Goods();
                if( !$unuse ) return $this->_Goods->_use($user, $id, $good, $idM);
                return $this->_Goods->_unuse($user, $id, $good);
            break;
            
            case "artifact":
                if(!$this->_Goods)  $this->_Goods = new _Goods();
                if( !$unuse ){
                    #_echo(1);
                    return $this->_Goods->_use($user, $id, "artifact", $idM);    
                }else{
                    #_echo(0);
                    return $this->_Goods->_unuse($user, $id, "artifact");    
                } 
            break;
            
            case "clothing":
                if(!$this->_Goods)  $this->_Goods = new _Goods();
                if( !$unuse ) return $this->_Goods->_use($user, $id, "clothing", $idM);
                return $this->_Goods->_unuse($user, $id, "clothing");
            break;
            
            case "potion":
                return $this->del($user, $good, $id);                
            break;
            
            case "seed":
                if(!$this->_Seed)  $this->_Seed= new _Seed();
                if( !$unuse ) return $this->_Seed->_use($user, $id);                                          
            break;
            
            case "food":                
                if(!$this->_Goods)  $this->_Goods = new _Goods();
                #if( !$unuse ) 
                return $this->_Goods->_use($user, $id, "food");
                #return $this->_Goods->_unuse($user, $id, "food");
                
                /*if(!$this->_Food)  $this->_Food = new _Food();                
                if( !$arr = $this->_Food->_use($user, $id)) return false ;
                return $arr;*/                
            break;
        }
        return true;
    }
    
    function getAll($user){
        $sql = "SLECT * FROM `".PREFIX_TABLE."` WHERE `user` = '".$user."'";
    }
    
                    
}
?>